package main

import (
	"database/sql"
	"encoding/json"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

const (
	driver = "mysql"
	dsn    = "root:root@tcp(127.0.0.1:3306)/users?charset=utf8"
)

func main() {
	var db, dbErr = sql.Open(driver, dsn)
	if dbErr == nil {
		fmt.Println("数据库打开成功")
	}
	defer db.Close()
	var sql = "select * from region where name like ?"
	var rows, queryErr = db.Query(sql, "%省%")
	if queryErr == nil {
		fmt.Println("sql执行成功")
	}
	defer rows.Close()
	var id int
	var name string
	var eng_name string
	var pid int
	var datas []map[string]interface{}
	var index int
	for rows.Next() {
		rows.Scan(&id, &name, &eng_name, &pid)
		/*
			var data = make(map[string]interface{})
			data["id"] = id
			data["name"] = name
			data["eng_name"] = eng_name
			data["pid"] = pid
		*/
		var data = map[string]interface{}{"id": id, "name": name, "eng_name": eng_name, "pid": pid}
		data["index"] = index
		index++
		datas = append(datas, data)
	}
	fmt.Println(datas[0])
	var dataJson, _ = json.Marshal(datas)
	fmt.Println(string(dataJson))
}
